/*
 * @Author: venusJin
 * @Date: 2018-04-02 18:33:03
 * @Last Modified by: jinxing@birdpush.com
 * @Last Modified time: 2018-04-03 11:47:41
 */
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
let maxAge = 0 * 60 * 60 * 1000

const state = {
  user: null
}

const getters = {

}

const mutations = {
  setUser (state, data) {
    state.user = data
    localStorage.setItem('user', JSON.stringify(data))
    localStorage.setItem('loginT', new Date().valueOf())
  },
  initUser (state) {
    if (localStorage.getItem('user')) {
      let data = JSON.parse(localStorage.getItem('user'))
      let t = Number(data.loginT)
      let t1 = new Date().valueOf()
      if (t1 - t > maxAge) {
        // 缓存过期
        localStorage.clear()
      } else {
        state.user = data
      }
    } else {
      state.user = null
    }
  }
}

const actions = {

}

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions
})
